<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class GoodsController extends Controller
{
    //商品详情
   public function goods_info($goods_id,$c_id)
   {
       //分配session信息到模板
       $user_info= session('sign_user_info');
       //商品详情
       $goods = DB::table('goods')
           ->where(['goods.status'=>1])
           ->where('goods.goods_id','=',$goods_id)
           ->join('img','goods.goods_id','=','img.goods_id')
           ->select('goods.*','img.img_src','img.status')
           ->get();
       //商品属性、属性值
       $property = DB::table('goods_property')
           ->where(['goods_property.goods_id'=>$goods_id])
           ->join('property','goods_property.property_id','=','property.p_id')
           ->join('property_value',function ($join){
               $join->on('goods_property.property_value','=','property_value.p_value')->on('goods_property.property_id','=','property_value.p_id');
           })
           ->select('goods_property.*','property.p_name','property_value.price')
           ->get();
       $collection = collect($property);
       $grouped = $collection->groupBy('p_name');
       $list = $grouped->toArray();

       //商品评价
       $comment = DB::table('comment')
           ->where(['goods_id'=>$goods_id])
           ->join('user','comment.user_id','=','user.id')
           ->select('comment.*','user.user_name')
           ->get();
       //同类商品信息
       $similar_goods = DB::table('goods')
           ->where(['goods.status'=>1])
           ->where('c_id','=',$c_id)
           ->leftJoin('img','goods.goods_id','=','img.goods_id')
           ->get();
       //商品分类表 分类名展示
       $classify = DB::table('classify')->get();

       return view('Home/Goods/goods_info')->with('goods',$goods)->with('similar_goods',$similar_goods)->with('list',$list)->with('classify',$classify)->with('comment',$comment)->with('user_info',$user_info);
   }

   //商品评论
    public function goods_comment(Request $request)
    {
        //分配session信息到模板
        $user_info= session('sign_user_info');
        $data = $request->input();
        if(empty($user_info['id']))
        {
            return ['status'=>0,'info'=>'请登录后参与评论'];
        }
        $res = DB::table('order')
            ->where(['user_id'=>$user_info['id']])
            ->where(['goods_id'=>$data['goods_id']])
            ->join('order_goods','order.id','=','order_goods.order_id')
            ->select('order.id','order.user_id','order_goods.goods_id')
            ->get();
        if(!$res)
        {
            return ['status'=>0,'info'=>'您还没有购买过该商品,不能参与评论哦'];
        }
        //  数据验证
        if(empty($data['quality_score']) || empty($data['price_score']))
        {
            return ['status'=>0,'info'=>'给商品打个分吧'];
        }
        if(!is_numeric($data['quality_score']) || !is_numeric($data['price_score']))
        {
            return ['status'=>0,'info'=>'请输入正确的评分'];
        }
        if(empty($data['comment']) || mb_strlen($data['comment'])<2 || mb_strlen($data['comment'])>50)
        {
            return ['status'=>0,'info'=>'评论内容字符长度应该在2-50个字符之间'];
        }
        //去重
        $re =DB::table('comment')
            ->where(['user_id'=>$user_info['id'],'goods_id'=>$data['goods_id']])
            ->first();
        if($re)
        {
            return ['status'=>0,'info'=>'您已经评论过该商品哦'];
        }

        $bool = DB::table('comment')->insert([
           'user_id'=>$user_info['id'],
            'goods_id'=>$data['goods_id'],
            'quality_score'=>$data['quality_score'],
            'price_score'=>$data['price_score'],
            'comment'=>$data['comment'],
        ]);
        if($bool)
        {
            return ['status'=>1,'info'=>'评论成功！'];
        }else{
            return ['status'=>0,'info'=>'评论失败'];
        }

    }

}
